package com.runlooker.runer.util;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author YUTANG
 * 
 */
public class TimeUtil {
	/**
	 * "yyyy-MM-dd HH:mm:ss" 12小时制
	 */
	public static final String FORMAT_DATETIME_12 = "yyyy-MM-dd hh:mm:ss";

	/**
	 * "yyyy-MM-dd HH:mm:ss" 24小时制
	 */
	public static final String FORMAT_DATETIME_24 = "yyyy-MM-dd HH:mm:ss";
	/**
	 * "yyyy-MM-dd"
	 */

	public static final String FORMAT_DATE1 = "yyyy-MM-dd";

	/**
	 * "yyyy-MM"
	 */
	public static final String FORMAT_DATE2 = "yyyy-MM";
	private final static String[] weekDays = { "星期日", "星期一", "星期二", "星期三",
			"星期四", "星期五", "星期六" };

	/**
	 * @param datetimeformate
	 *            "yyyy-MM-dd" "yyyy-MM-dd hh:mm:ss"
	 * @return 根据传入的时间格式，返回时间字符串
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getCurrenDateTime(String datetimeformate) {
		String currentTimestr = null;
		SimpleDateFormat sDateFormat = new SimpleDateFormat(datetimeformate);
		Date curDateTime = new Date(System.currentTimeMillis());
		currentTimestr = sDateFormat.format(curDateTime);
		return currentTimestr;
	}

	/**
	 * 返回系统毫秒值
	 * 
	 * @return
	 */
	public static String getCurrentMillisecond() {
		return String.valueOf(System.currentTimeMillis());
	}

	/**
	 * 根据传入的时间，返回是星期几
	 * 
	 * @Title: getWeekIndexByDate
	 * @param str
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static int getWeekIndexByDate(String str) {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		int weeks = -1;
		try {
			date = dateFormatter.parse(str);
			dateFormatter.applyPattern("w");
			weeks = Integer.valueOf(dateFormatter.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return weeks;
	}

	/**
	 * 根据传入的时间字符串，返回毫秒值
	 * 
	 * 
	 * @Title: getMillisecondByDate
	 * @Description:
	 * @param date
	 * @param dateFormat
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static long getMillisecondByDate(String date, String dateFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		try {
			long millionSeconds = sdf.parse(date).getTime();
			return millionSeconds;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return -1L;
	}

	/**
	 * 
	 * 根据毫秒值和时间格式，返回时间字符串"yyyy-MM-dd""yyyy-MM-dd hh:mm:ss""
	 * 
	 * @param date
	 * @param dateFormat
	 * @return
	 */
	public static String getDateByMillisecond(String millionSeconds,
			String dateFormat) {
		String currentTimestr = null;
		SimpleDateFormat sDateFormat = new SimpleDateFormat(dateFormat);
		Date curDateTime = new Date(Long.valueOf(millionSeconds));
		currentTimestr = sDateFormat.format(curDateTime);
		return currentTimestr;
	}

	/**
	 * 
	 * 根据日期，返回星期几
	 * 
	 * @Title: getwhatDayByDate
	 * @param str
	 * @return
	 */
	public static String getWeekdayByDate(String date) {
		long milliseconds = getMillisecondByDate(date, "yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(milliseconds));
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekDays[w];
	}

	/**
	 * 
	 * 根据日期，返回是第几月份
	 * 
	 * @param date
	 */
	public static int getMonthByDate(String date) {
		long milliseconds = getMillisecondByDate(date, "yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(milliseconds));
		return (cal.get(Calendar.MONTH) + 1);
	}

	/**
	 * 
	 * 当前系统时间是星期几
	 * 
	 * @Title: getCurrentWeek
	 * @return
	 */
	public static String getCurrentWeekday() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekDays[w];
	}

}
